package com.android.pinprovince.fragment.member;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.pinprovince.R;
import com.android.pinprovince.fragment.BaseLazyFragment;
import com.android.pinprovince.model.data;
import com.android.pinprovince.model.dataCallback;
import com.android.pinprovince.utils.ConstantsUrl;
import com.android.pinprovince.utils.HttpUtils;
import com.android.pinprovince.utils.ObjectUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


/**
 * 反馈
 */
public class Fragment_Mine_Feedback extends BaseLazyFragment {

    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.et_phone_num)
    EditText etMobil;
    @BindView(R.id.et_email)
    EditText etEmail;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.tv_sure)
    TextView sure;
    @BindView(R.id.tv_cancel)
    TextView cancel;

    private FragmentActivity mcontext;
    private View view;
    private LayoutInflater inflater;
    private boolean networkConnected;
    private String mobile;
    private String email;
    private String content;

    public Fragment_Mine_Feedback() {
    }

    public static Fragment_Mine_Feedback newInstance() {
        Fragment_Mine_Feedback fragment = new Fragment_Mine_Feedback();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }

    @Override
    protected void initPrepare() {
        initUI();
    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_mine_feedback, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }

    private void initUI() {
        progress.setVisibility(View.GONE);
    }

    @OnClick({R.id.tv_cancel, R.id.tv_sure
    })
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                getActivity().finish();
                break;
            case R.id.tv_sure:
                if (isEdited()) {
                    toOpinion();
                } else {
                    ObjectUtils.toast(mcontext, "请输入完整信息");
                }
                break;
        }
    }

    private boolean isEdited() {
        mobile = etMobil.getText().toString().trim();
        email = etEmail.getText().toString().trim();
        content = etContent.getText().toString();
        if (TextUtils.isEmpty(mobile) || TextUtils.isEmpty(email) || TextUtils.isEmpty(content)) {
            return false;
        } else {
            return true;
        }
    }

    private void toOpinion() {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.MEMBERFEEDBACK;
            HashMap<String, String> map = new HashMap<>();
            map.put("mobile", mobile);
            map.put("email", email);
            map.put("content", content);
            progress.setVisibility(View.VISIBLE);
            HttpUtils.get(address, map, mCallback);
        } else {
            ObjectUtils.toast(mcontext, "网络连接出现异常");
        }
    }

    dataCallback mCallback = new dataCallback() {

        @Override
        public void onError(Call call, Exception e, int id) {
            progress.setVisibility(View.GONE);
            ObjectUtils.toast(mcontext, "提交失败");
        }

        @Override
        public void onResponse(data response, int id) {
            progress.setVisibility(View.GONE);
            if (response.getCode() == 0) {
                new AlertDialog.Builder(mcontext)
                        .setMessage("提交成功")
                        .setPositiveButton("确定", null)
                        .show();
                etMobil.setText("");
                etEmail.setText("");
                etContent.setText("");
            } else {
                ObjectUtils.toast(mcontext, response.getMsg());
            }
        }
    };
}
